<template>
  <div>
    <a-modal title="自定义价格" :width="928" :visible="visible" :confirm-loading="confirmLoading" @ok="handleOk" @cancel="handleCancel">
      <div class="option_box">
        <a-select class="opt_w mrg_r" style="width:140px;" v-model="priceType" @change="handlePrice">
          <a-select-option :value="1"> 淘宝原价 </a-select-option>
          <a-select-option :value="2"> 聚麦促销最低价 </a-select-option>
        </a-select>
        <a-select class="opt_w mrg_r" v-model="discType" @change="handlePrice">
          <a-select-option :value="1"> 批量打折 </a-select-option>
          <a-select-option :value="2"> 批量减价 </a-select-option>
        </a-select>
        <a-input-number v-if="discType === 1" type="number" class="opt_w mrg_r" :min="1" :max="10" :step="0.01" v-model="discNum" :placeholder="c_placeholder" @change="handlePrice" />
        <a-input-number v-else type="number" class="opt_w mrg_r" :min="1" v-model="discNum" :placeholder="c_placeholder" @change="handlePrice" />
        <a-button class="mrg_r" type="primary" ghost @click="onClean(0)">抹 角</a-button>
        <a-button type="primary" ghost @click="onClean(1)">抹 分</a-button>
      </div>
      <div class="goods_box">
        <div class="goods_item" v-for="item in goodsList" :key="item.numIid">
          <img :src="item.picUrl" alt="">
          <div class="item_info">
            <div class="title">{{ item.title }}</div>
            <div class="price">{{ item.price }} 元</div>
          </div>
          <a-input class="inp" type="number" v-model="item.markPrice" />
          <!-- <a-input-number class="inp" :value="item.markPrice" /> -->
        </div>
      </div>
      <!-- <pPagination v-if="pagination.total" :showSizeChanger="false" :totalData="pagination.total" :pageNumber="pagination.pageNumber" :pageSizeProp="pagination.pageSize" @onPageChange="onPageChange"></pPagination> -->
    </a-modal>
  </div>
</template>

<script>
// import pPagination from '@/components/p-pagination'
import { mapState } from 'vuex'
export default {
  // components: { pPagination },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    ...mapState(['XShopItems']),
    c_placeholder () {
      return this.discType === 1 ? '请输入折扣' : '请输入减价'
    }
  },
  data () {
    const pagination = {
      total: 1,
      pageSize: 10,
      pageNumber: 1
    }
    return {
      pagination,
      confirmLoading: false,
      priceType: 2, // 1 淘宝原价 2 聚麦促销最低价
      discType: 1, // 1 批量打折 2批量减价
      goodsList: [],
      discNum: ''
    }
  },
  methods: {
    onPageChange (pageNumber, pageSize) {
      this.pagination.pageNumber = pageNumber
      this.pagination.pageSize = pageSize
      this.getDataList()
    },
    handleOk () {
      this.goodsList.map(item => {
        item.custmPrice = item.markPrice
      })
      this.$emit('ok', this.goodsList)
    },
    handleCancel () {
      this.$emit('close')
    },
    handlePrice () {
      this.goodsList.map(item => {
        const priceKey = this.priceType === 1 ? 'price' : 'lowestPrice'
        let num = this.discNum
        if (!num) num = this.discType === 1 ? 10 : 0
        if (this.discType === 1) item.markPrice = (item[priceKey] * (num * 0.1)).toFixed(2)
        if (this.discType === 2) item.markPrice = item[priceKey] - num
      })
    },
    // 抹 零
    onClean (fixedNum) { // 0抹角 1 抹分
      this.goodsList.map(item => {
        const priceKey = this.priceType === 1 ? 'price' : 'lowestPrice'
        let num = this.discNum
        if (!num) num = this.discType === 1 ? 10 : 0
        if (this.discType === 1) item.markPrice = (item[priceKey] * (num * 0.1)).toFixed(fixedNum)
        if (this.discType === 2) item.markPrice = (item[priceKey] - num).toFixed(fixedNum)
      })
      this.$forceUpdate()
    }
  },
  watch: {
    XShopItems: {
      handler (newValue) {
        this.goodsList = newValue
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style lang="less" scoped>
.option_box {
  .mrg_r {
    margin-right: 12px;
  }
  .opt_w {
    width: 120px;
  }
}
.goods_box {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  background-color: #f8f8f8;
  border: 1px solid #e8e8e8;
  border-bottom: none;
  .goods_item {
    width: 439px;
    padding: 14px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #e8e8e8;
    &:hover {
      background-color: #e9f6fe;
    }
    &:nth-of-type(odd) {
      border-right: 1px solid #e8e8e8;
    }
    img {
      width: 60px;
      height: 60px;
    }
    .item_info {
      margin: 0 14px;
      .title {
        width: 211px;
        height: 36px;
        font-size: 14px;
        color: #1890ff;
        line-height: 18px;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        cursor: pointer;
      }
      .price {
        margin-top: 8px;
        color: #6e6e6e;
      }
    }
    .inp {
      width: 115px;
      font-weight: bold;
      color: #555555;
    }
  }
}
</style>
